import pandas



def eliminate_anomaly_price(model_data):
    '''
    消除异常价格，sku_level维度
    去除最大值和最小值
    '''
    model_data.sort_values(by = ["sku_level", "item_quotation_price_num"])

    model_data_grouped = model_data.groupby('sku_level')['item_quotation_price_num']

    model_data['rank1'] = model_data_grouped.rank(ascending=1, method='first').astype(int)
    model_data['rank2'] = model_data_grouped.rank(ascending=0, method='first').astype(int)

    model_data_size_data = model_data.groupby('sku_level').size().reset_index()
    model_data_size_data.columns = ['sku_level', 'size']

    model_data = pandas.merge(model_data, model_data_size_data, on='sku_level')

    model_data_v2 = model_data[~((model_data['size'] >= 5) & ((model_data['rank1'] == 1) | (model_data['rank2'] == 1)))]

    return model_data_v2



